package Com.HupochuanAdmin.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.Area;

public class AreaTab {
	/*
	 * 本类私有对象
	 */
	private DBManager<Area> dbManager = new DBManager<Area>(Area.class);
	
	/*
	 * 单一实例
	 */
	private AreaTab(){}
	private static AreaTab areaTab = new AreaTab();
	public static AreaTab getObject(){
		return areaTab;
	}
	
	/*
	 * 获得地区列表
	 */
	public List<Area> findAreaList(){
		//参数部分
		List<Area> areaList = new Vector<Area>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select Area.*, ");
		sqlBuff.append(" 	AreaSort.name as sortName, ");
		sqlBuff.append(" 	( ");
		sqlBuff.append(" 		select count(id) from Movie ");
		sqlBuff.append(" 		where Movie.areaId=Area.id ");
		sqlBuff.append(" 	) as movieCount ");
		sqlBuff.append(" from Area ");
		sqlBuff.append(" left join AreaSort on Area.sortId=AreaSort.id ");
		sqlBuff.append(" order by AreaSort.weight,id ");
		
		//执行sql 
		areaList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(), paraHash);
		
		//返回列表
		return areaList;
	}
	
	/*
	 * 获得地区列表
	 */
	public List<Area> findAreaList(long sortId){
		//参数部分
		List<Area> areaList = new Vector<Area>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select * from Area where sortId=${sortId} ");
		
		//sql参数部分
		paraHash.put("sortId",sortId);
		
		//执行sql 
		areaList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(), paraHash);
		
		//返回列表
		return areaList;
	}
	
	/*
	 * 获得地区ID
	 */
	public long findAreaId(String name){
		//参数部分
		long areaId = 0;
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select id from Area where name=${name} ");
		
		//sql参数部分
		paraHash.put("name",name);

		//执行sql
		areaId = dbManager.querySqlReturnLong(sqlBuff.toString(), paraHash);
		
		//返回
		return areaId;
	}
}
